package com.css.zfzx.sjcj.modules.hddcStation.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.hddcStation.repository.entity.HddcStationEntity;
import com.css.zfzx.sjcj.modules.hddcStation.service.HddcStationService;
import com.css.zfzx.sjcj.modules.hddcStation.viewobjects.HddcStationQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author zyb
 * @date 2020-11-28
 */
@Slf4j
@RestController
@RequestMapping("/hddc/hddcStation")
public class HddcStationController {
    @Autowired
    private HddcStationService hddcStationService;

    @GetMapping("/queryHddcStations")
    public RestResponse queryHddcStations(HttpServletRequest request, HddcStationQueryParams queryParams) {
        RestResponse response;
        try{
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String sort= request.getParameter("sort");
            String order= request.getParameter("order");
            JSONObject jsonObject = hddcStationService.queryHddcStations(queryParams, curPage, pageSize, sort, order);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getHddcStation(@PathVariable String id) {
        RestResponse response = null;
        try{
            HddcStationEntity hddcStation = hddcStationService.getHddcStation(id);
            response = RestResponse.succeed(hddcStation);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping("/save")
    public RestResponse saveHddcStation(@RequestBody HddcStationEntity hddcStation) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcStationService.saveHddcStation(hddcStation);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PostMapping("/update")
    public RestResponse updateHddcStation(@RequestBody HddcStationEntity hddcStation)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcStationService.updateHddcStation(hddcStation);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping("/delete")
    public RestResponse deleteHddcStations(@RequestBody String ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcStationService.deleteHddcStations(Arrays.asList(ids.split(",")));
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(hddcStationService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }
    /***
     * 导出
     * @param response
     * @return
     */
    @GetMapping("/exportFile")
    public RestResponse exportFileYhDisasters(HttpServletResponse response,
                                              @RequestParam("projectName")String projectName,@RequestParam("stationname") String stationname,
                                              @RequestParam("province") String province,@RequestParam("cityCode")String cityCode,@RequestParam("area")String area) {
        RestResponse responseRest = null;
        JSONObject jsonObject = new JSONObject();
        try{
            HddcStationQueryParams queryParams=new HddcStationQueryParams();
            queryParams.setArea(area);
            queryParams.setCityCode(cityCode);
            queryParams.setProvince(province);
            queryParams.setProjectName(projectName);
            queryParams.setStationname(stationname);
            hddcStationService.exportFile(queryParams,response);
            jsonObject.put("message", "导出成功");
            responseRest = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            responseRest = RestResponse.fail(errorMessage);
        }
        return responseRest;
    }

    /**
     * 导入
     *
     * @param file
     * @param response
     * @return
     */
    @PostMapping("/importDisaster")
    public RestResponse export(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = hddcStationService.exportExcel( file, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}